/** *************************************************************************
 *  @file       net_init.c
 *  @brief	    网络初始化函数
 *  @date		2021/04/23 
 *  @author		junshuai.li
 *  @version	0.0.1
 ****************************************************************************/
#include "net_init.h"
#include "../basic_cfg/basic_cfg.h"


int parse_tsnlight_info(char* buf,u16 *tsnlight_mid)
{
	int ret = 0;
	u16 tvalue16 = 0;

	
	cJSON *root_json=NULL;
	cJSON *network_init_cfg_json=NULL;
	cJSON *tsnlight_json=NULL;
	cJSON *tmp_json=NULL;
	
	riprt_n(buf);
	root_json=cJSON_Parse(buf);

	if (!root_json) 
	{
		printf("parse root_json error, Error before: [%s]\n",cJSON_GetErrorPtr());
		return -1;
	}
	
	network_init_cfg_json = cJSON_GetObjectItem(root_json, "network_init_cfg");
	if (!network_init_cfg_json) 
	{
		printf("parse network_init_cfg error, Error before: [%s]\n",cJSON_GetErrorPtr());
		cJSON_Delete(root_json);
		return -1;
	}

	tsnlight_json = cJSON_GetObjectItem(network_init_cfg_json, "tsnlight");
	if (!tsnlight_json) 
	{
		printf("parse tsnlight_json error, Error before: [%s]\n",cJSON_GetErrorPtr());
		cJSON_Delete(root_json);
		return -1;
	}
	else
	{
		tmp_json = cJSON_GetObjectItem(tsnlight_json, "tsnlight_mid");
		if (!tmp_json) 
		{
			printf("parse tsnlight_mid error, Error before: [%s]\n",cJSON_GetErrorPtr());
			cJSON_Delete(root_json);
			return -1;
		}
		else
		{
			sscanf(tmp_json->valuestring, "%hx", &tvalue16);
			*tsnlight_mid = tvalue16;
			printf("tsnlight_mid = 0x%hx \n", *tsnlight_mid);
		}

	}

	cJSON_Delete(root_json);
	
    return ret;
	
}


int get_tsnlight_info_from_file(u8 *cfg_file_name, u16 *tsnlight_mid)
{
	int ret = 0;
	
	FILE *fp;
	char *buf;
	fp = fopen(cfg_file_name,"r");

	if(fp == NULL)
	{
		printf("fopen %s error\n",cfg_file_name);
		return -1;
	}
			
    // 确定文件大小, 并将读取指针移回文件开始
    fseek(fp, 0, SEEK_END);
    long pos = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    if (pos <= 0) 
	{
        printf("%s Invalid!\n",cfg_file_name);
		fclose(fp);
		return -1;
    }
	
    // 根据文件大小申请内存空间, 注意这边要多申请1个字节用于存放'\0'
    buf = (char*)malloc(pos + 1);
    if (buf == NULL) {
        printf("No enough memory.");
		fclose(fp);
		return -1;
    }
	bzero(buf, pos + 1);
	
    // 读取文件内容至buff
    int nread = fread(buf, pos, 1, fp);
    if (!nread) {
        printf("Failed to read the config file.");
		free(buf);
		fclose(fp);
		return -1;
    }

    //解析json，获取tsnlight的mid信息
	ret = parse_tsnlight_info(buf,tsnlight_mid);

	if(buf != NULL)
	{
		free(buf);
	}
	
    // 关闭文件
	fclose(fp);

	return ret;
}


int net_init(u8 *network_inetrface,u16 *tsnlight_mid,u32 *sync_mode,u32 version)
{
	int ret = 0;
	u8 local_mac[6] = {0};
	u8 *init_cfg_file_name = NULL;
	

	//初始化libnet和libpcap
	//char test_rule[256] = {0};
	//sprintf(test_rule,"%s","(ether[0:2]=0x6626 && ether[2]=0x62 && ether[5]=0x1 && ether[12:2]=0xff01) || (ether[12:2]=0x0800 && ether[23]=0x11 && ether[36:2]=0x1f99)");

	/*配置过滤器*/
	struct sock_fprog filter;

#if 1
    struct sock_filter code[] = {
    { 0x28, 0, 0, 0x00000000 },
	{ 0x15, 0, 6, 0x00006626 },
	{ 0x30, 0, 0, 0x00000002 },
	{ 0x15, 0, 4, 0x00000062 },
	{ 0x30, 0, 0, 0x00000005 },
	{ 0x15, 0, 2, 0x00000001 },
	{ 0x28, 0, 0, 0x0000000c },
	{ 0x15, 6, 0, 0x0000ff01 },
	{ 0x28, 0, 0, 0x0000000c },
	{ 0x15, 0, 5, 0x00000800 },
	{ 0x30, 0, 0, 0x00000017 },
	{ 0x15, 0, 3, 0x00000011 },
	{ 0x28, 0, 0, 0x00000024 },
	{ 0x15, 0, 1, 0x00001f99 },
	{ 0x06, 0, 0, 0x0000ffff },
	{ 0x06, 0, 0, 0x00000000 },
    };
	
	filter.len = 16;
    filter.filter = code;
#endif
	//data_pkt_receive_init(test_rule,network_inetrface);//数据接收初始化
	ret = tsmp_pkt_send_recv_init(network_inetrface,filter);//数据发送接收初始化
	if(ret == -1)
	{
		printf("tsmp_pkt_send_recv_init fail\n");
		return -1;
	}
	tsninsight_init();//TSNInsight_init通信初始化
	
	init_cfg_file_name = INIT_CFG_FILE;//初始配置文本
	ret = get_tsnlight_info_from_file(init_cfg_file_name,tsnlight_mid);
	if (ret == -1)
	{
		printf("get_tsnlight_info_from_file fail \n");
		return -1;
	}
	
	*sync_mode = 1; // 1代表PTP
	printf("get tsnlight_mid %d\n",*tsnlight_mid);
	printf("get sync_mode %d\n",*sync_mode);
	set_controller_mac(*tsnlight_mid,TSNLIGHT_APP);

	//获取TSNLight的Mac地址,TSNLight_mid是解析tsnlight_init_cfg.xml中获取的
	get_controller_mac_from_mid(local_mac,*tsnlight_mid,TSNLIGHT_APP);
	fwrite_file(local_mac,version,"TSNLight");
	
	
	//初始化完成，跳转到基础配置状态
	printf("*************enter BASIC_CFG_S********************\n");
	
	return 0;

}


int resource_clear(u8 *network_inetrface)
{
	tsmp_pkt_send_recv_destroy();
	tsninsight_destroy();

	return 0;
}


